package de.hauck.rtmidlet.views;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;

import de.hauck.rtmidlet.FileSystemExportThread;
import de.hauck.rtmidlet.FileSystemImportThread;
import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockStore;
import de.hauck.rtmidlet.utils.FileChooser;

public class FormStockMaintainMenu extends Form implements CommandListener {
	private RTMidlet _main;
	private StockStore _myStore;
	// private Display _display;
	private List editStocksMenu;
	private List stockList;
	private FormSettingsMenu _fm;
	private final Command cOK = new Command("OK", Command.OK, 1);
	private final Command cCancel = new Command("Cancel", Command.BACK, 1);
	private Command cEditStock = new Command("Edit", Command.OK, 1);
	private Command cDeleteStock = new Command("Delete", Command.OK, 1);
	private Command cConfirmDeleteYes = new Command("Yes", Command.OK, 1);
	private Command cConfirmDeleteNo = new Command("No", Command.BACK, 1);
	private Command cImportYes = new Command("Yes", Command.OK, 1);
	private Command cImportNo = new Command("No", Command.BACK, 1);

	private Command cConfirmOK = new Command("OK", Command.OK, 1);
	
	private String chosenFile="";
	public FormStockMaintainMenu(FormSettingsMenu fm, StockStore mystore) {
		super("Edit stocks");
		_fm = fm;
		_myStore = mystore;
		// _display = display;
		_main = RTMidlet.getInstance();

		String[] editStockMenuElements = { "Add stock...", "Edit stock...",
				"Delete stock...","Export to CSV","Import from CSV" };
		editStocksMenu = new List("Maintain stocks", Choice.IMPLICIT,
				editStockMenuElements, null);
		editStocksMenu.addCommand(cOK);
		editStocksMenu.addCommand(cCancel);
		editStocksMenu.setSelectCommand(cOK);
		editStocksMenu.setCommandListener(this);
	}

	public void commandAction(Command command, Displayable dble) {
		if (command == cCancel) {
			_main.show(_fm.getSettingsMenu());
		} else if (command == cOK) {
			switch (editStocksMenu.getSelectedIndex()) {
			case 0: {
				// add stock
				// create new FormStockMaintain with empty data
				_main.show(new FormStockMaintain(0, _myStore, _main, this, 0));
				// _display.setCurrent(new FormStockMaintain(_display, 0,
				// _myStore, _main, this, 0));
				break;
			}
			case 1: {
				// edit stock
				// present stock chooser list
				setTitle("Choose stock to edit");
				stockList = new List("Portfolio", Choice.IMPLICIT, _myStore
						.getNameArray(-1), null);
				System.out.println(stockList.size());
				stockList.addCommand(cEditStock);
				stockList.addCommand(cCancel);
				stockList.setSelectCommand(cEditStock);
				stockList.setCommandListener(this);
				_main.show(stockList);
				break;
			}
			case 2: {
				// delete stock
				// present stock chooser list
				setTitle("Choose stock to delete");
				stockList = new List("Portfolio", Choice.IMPLICIT, _myStore
						.getNameArray(-1), null);
				System.out.println(stockList.size());
				stockList.addCommand(cDeleteStock);
				stockList.addCommand(cCancel);
				stockList.setSelectCommand(cDeleteStock);
				stockList.setCommandListener(this);
				_main.show(stockList);
				break;
			}
			case 3: {
				// Export to CSV
				if (Configuration.exportPath!=null) {
				new FileSystemExportThread(_myStore, this,1);
				} else RTMidlet.getInstance().alert("Info", "No path to export selected", Configuration.ALERT_WAIT_TIME);
				break;
			}
			case 4: {
				// Import from CSV
				FileChooser fchoser = new FileChooser(null,true,this);
				_main.show(fchoser);
				break;
			}
			}
		} else if (command == cEditStock) {
			FormStockMaintain fm = new FormStockMaintain(stockList
					.getSelectedIndex(), _myStore, _main, this, 1);
			_main.show(fm);
			// _display.setCurrent(fm);
		} else if (command == cDeleteStock) {
			Alert delete_confirm = new Alert("Delete",
					"Do you really want to delete "
							+ stockList.getString(stockList.getSelectedIndex())
							+ "?", null, AlertType.CONFIRMATION);
			delete_confirm.addCommand(cConfirmDeleteYes);
			delete_confirm.addCommand(cConfirmDeleteNo);
			delete_confirm.setCommandListener(this);
			_main.show(delete_confirm);
		} else if (command == cConfirmDeleteYes) {
			_myStore.delStock(stockList.getSelectedIndex());
			stockList.delete(stockList.getSelectedIndex());
			_main.show(stockList);
		} else if (command == cConfirmDeleteNo) {
			_main.show(stockList);
		} else if (command == cImportYes) {
			new FileSystemImportThread(0, chosenFile, _myStore,  this);
			Configuration.dirtyFlagStocks=true;
			_main.show(this);
		} else if (command == cImportNo) {
			_main.show(this);
		} else if (command==cConfirmOK) {
			_main.show(editStocksMenu);
		}
	}

	public List getEditStocksMenu() {
		return editStocksMenu;
	}
	
	public void showImportConfirmation(String file) { 
		chosenFile=file;
		Alert import_confirm = new Alert(
				"Import",
				"Do you really want to import "+file+"? Database will be deleted!",
				null, AlertType.CONFIRMATION);
		import_confirm.addCommand(cImportYes);
		import_confirm.addCommand(cImportNo);
		import_confirm.setCommandListener(this);	
		_main.show(import_confirm);
	}
	
	public void showImportSuccess() {
		Alert import_confirm = new Alert(
				"Import",
				"Import was successfull.",
				null, AlertType.INFO);
		import_confirm.addCommand(cConfirmOK);
		import_confirm.setCommandListener(this);
		_main.show(import_confirm);
	}
	
	public void showExportSuccess() {
		Alert export_confirm = new Alert(
				"Export",
				"Export was successfull.",
				null, AlertType.INFO);
		export_confirm.addCommand(cConfirmOK);
		export_confirm.setCommandListener(this);
		_main.show(export_confirm);
	}

}
